package com.burgeon.backend.EnterpriseWeChat.rest.integral;

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetGoodsKuCun extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetGoodsKuCun(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public HashMap getData(String code) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(code), url);
        System.err.println(str);
        String code1 = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code1)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        if(jsonArray.length()>0){
            HashMap map = new HashMap();
            map.put("price",jsonArray.getJSONArray(0).getString(0));
            map.put("inventory",jsonArray.getJSONArray(0).getString(1));

            return map;
        }
        return null;
    }

    private MultiValueMap Data(String code) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("'M_PRODUCT_ID;PRICELIST'");//标准价
        columns.add(" QTYCAN");//可配

        params.put("columns", columns);
        params.put("table", "FA_STORAGEYC_SKU");//表名
        params.put("count","true");

        HashMap paramss = new HashMap();
        String ac = "'="+code+"'";
        paramss.put("column", "'M_PRODUCT_ID;NAME'");
        paramss.put("condition", ac);

        params.put("params", paramss);
        System.err.println(params);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetGoodsKuCun g = new GetGoodsKuCun("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(g.getData(""));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
